
	/***********************************************************************************************

		Module: Encode (native api)
		
		Methods:
	      mod __ni_xml_parse	(source, callback(name, type, value, attributes, level))	- sid: 1.0

 	************************************************************************************************/
	
	/*
	  Native Node types:
	  
		1 	ELEMENT_NODE
		2 	ATTRIBUTE_NODE
		3 	TEXT_NODE
		4 	CDATA_SECTION_NODE
		5 	ENTITY_REFERENCE_NODE
		6 	ENTITY_NODE
		7 	PROCESSING_INSTRUCTION_NODE
		8 	COMMENT_NODE
		9 	DOCUMENT_NODE
		10 	DOCUMENT_TYPE_NODE
		11 	DOCUMENT_FRAGMENT_NODE
		12 	NOTATION_NODE
	*/	
	//-------------------------------------------------------------------------------
	jsl.Encode.__ni_xml_parse = function(source, callback) {	
		try {
			var parser, node, e
			
			//@d[		
			if(arguments.length !== 2)	throw jsl.ParamError()
			jsl.validate(source		, "s+", "source")
			jsl.validate(callback	, "f"	, "callback")			
			//@d]

			if(jsl.__stype === "cli") {

				DOM 		= require("dom")
				parser 	= new DOM.DOMParser()
				node 		= parser.parseFromString(source);			

				//@d[
				if(node.documentElement && node.documentElement.nodeName === "parsererror")
      		throw jsl.ParamError("SyntaxError:source", node.documentElement.childNodes[0].nodeValue)//@d]								
			}
			else if(window.DOMParser){
			  
				parser 	= new DOMParser()
			  node		= parser.parseFromString(source,"text/xml")
				
				//@d[
				//1° test
				if(	node.documentElement 																		&& 
						node.documentElement.nodeName === "html" 								&&
						node.documentElement.childNodes[0]											&&
						node.documentElement.childNodes[0].nodeName === "body" 	&&
						node.documentElement.childNodes[0].childNodes[0] 				&&
						node.documentElement.childNodes[0].childNodes[0].nodeName === "parsererror")
      		throw jsl.ParamError("SyntaxError:source")					
						
				//2° test
				if(node.documentElement && node.documentElement.nodeName === "parsererror")
      		throw jsl.ParamError("SyntaxError:source", node.documentElement.childNodes[0].nodeValue)//@d]								
				
				//3° test
				if(	node.documentElement 																		&& 
						node.documentElement.nodeName === "html" 								&&
						node.documentElement.childNodes[0]											&&
						node.documentElement.childNodes[0].nodeName === "body" 	&&
						node.documentElement.childNodes[0].childNodes[0] 				&&
						node.documentElement.childNodes[0].childNodes[0].nodeName === "h1" 	&&
						node.documentElement.childNodes[0].childNodes[0].childNodes[0]			&&
						node.documentElement.childNodes[0].childNodes[0].childNodes[0].nodeValue.match(/xml\s+parsing\s+error/i))
      		throw jsl.ParamError("SyntaxError:source")					
				
			}
			else {
			  
				node 				= new ActiveXObject("Microsoft.XMLDOM")
			  node.async 	= "false"
			  node.loadXML(source)
				//@d[
				if(node.parseError && node.parseError.errorCode != 0) {
					
					e = jsl.ParamError("SyntaxError:source")
					e.setLine(node.parseError.line)
					
					if(node.parseError.linePos)
						e.setChar(node.parseError.linePos)
					
					throw e.setCause(node.parseError.reason)				
				}//@d]
			} 
			//@d[
			if(!node.documentElement)	throw jsl.ParamError("SyntaxError:source")//@d]

			arguments.callee.__walk(node, callback, 0)						

			return this
		}
		catch(e) {
			jsl.throwError(e, this, arguments)
		}	
	}		
	//-------------------------------------------------------------------------------
	jsl.Encode.__ni_xml_parse.__walk = function(node, callback, level) {	
		try {			
			var attributes, a, l, key, attribute
			
			attributes	= {}
			l 					= node.attributes ? node.attributes.length : 0		
			
			for(a = 0; a < l; a++) {				
				//@sys: V8cgi - throws an error if used node.attributes.item(a) -> Object Attribute version,Attribute 
				//			updateSequence has no method 'item'
				attribute				= node.attributes[a] || node.attributes.item(a)
				key 						= attribute.nodeName
				attributes[key] = attribute.nodeValue
			}
						
			callback(node.nodeName, node.nodeType, node.nodeValue, attributes, level)
						
			for(a = 0, l = node.childNodes.length; a < l; a++)
				arguments.callee(node.childNodes[a], callback, level + 1)						
		}
		catch(e) { 
			jsl.throwError(e, this, arguments)
		}	
	}			

	